class Menu {
  constructor(el) {
    this.el = document.getElementsByClassName(el)
  }
  empty(css, type) {
    const arrayList = Array.from(this.el)
    arrayList.forEach(item=> {
      const isChilren = item.children
      if (isChilren.length && type) {
        this.childrenEmpty(isChilren, css)
      } else {
        this.targetAllEmpty(item, css)
      }
    })
  }

  childrenEmpty (isChilren, css) {
    const childrenList = Array.from(isChilren)

    childrenList.forEach(item=> {
      item.className = css
    })
  }

  targetAllEmpty(item, css) {
    item.className = css
  }

  /**
   * 
   * @param {*} defaults 所有slot中的url
   * @returns 
   */
  getAllSlotUrl(defaults) {
    const routerList = []
    const recursive = (defaults) => {
      defaults.forEach(item => {
        const children = item.children
        if (item.props) {
          routerList.push(item.props.url)
        }
        if (children.default) {
          const childrenList = children.default()
          recursive(childrenList)
        }
      })
    }
    recursive(defaults)
    return routerList
  }

  /**
   * 
   * @param {*} target 当前路由地址
   * @param {*} nextTick nextTick Callack
   */
  selectedTabBar(target, nextTick) {
    nextTick(() => {
      const tabBarElements = Array.from(this.el)

      tabBarElements.forEach(item => {
        const subMenuIs = item.children[1].children
        if (subMenuIs.length) {
          // 说明菜单下栏有子选项
          this.targetShow(subMenuIs, target)
        } else {
          item.className += item.className.indexOf(target) !== -1 ? ' selected' : ''
        }
      })
    })
  }

  targetShow (subMenuIs, target) {
    for(let i = 0; i < subMenuIs.length; i++) {
      const items = subMenuIs[i]
      if (items.className.indexOf(target) !== -1) {
        items.className += ' selected'
      }
    }
  }
}

export default Menu